﻿@using Telerik.Web.Mvc
@using Telerik.Web.Mvc.UI
@using Presentation.Web.Admin
<link href="@Url.Content("~/Content/colorbox.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery.colorbox.js")" type="text/javascript"></script>
@model  IEnumerable<Domain.Dto.TourDto>
@{
    ViewBag.subtitle = "推荐线路管理";
    ViewBag.title = "推荐线路管理";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@{ Html.Telerik().Window()
        .Name("TourAddWindow")
        .Title("添加线路")
        .Content(@<text>
    @Html.Partial("_Add", new Domain.Dto.TourDto())
    </text>)
        .Width(700)
        .Draggable(true).Modal(true).Visible(false)
        .Render();
}
@{ Html.Telerik().Window()
        .Name("TourAddPicWindow")
        .Title("添加配图")
        .Content(@<text>
    @Html.Partial("_TourPicUpload")
    </text>)
        .Width(500)
        .Draggable(true).Modal(true).Visible(false)
        .Render();
}
@{ Html.Telerik().Window()
        .Name("TourPicPreViewWindow")
        .Title("浏览图片")
        .Content(@<text>
    <div id="divTourPicPreView">
        &nbsp;
        <div style="display: none;">
            i am sum
        </div>
    </div>
    </text>)
        .Draggable(true).Modal(true).Visible(false)
        .Render();
}
@{Html.Telerik().Grid(Model)
    .Name("TourGrid")
    .ToolBar(toolBar => toolBar.Template(
    @<text>
    @{
        if (Html.HasUiPermission(this.ViewContext, "search"))
        {
        <label class="customer-label" for="Customers-input">
            线路名搜索:
        </label>
        <input type="text" id="searchTour" value="线路名" data-autocomplete="/tour/search" />
        <input type='button' value='我要搜索' class='t-button' onclick='searchChange()' />
        }
        if (Html.HasUiPermission(this.ViewContext, "add"))
        { 
        <a href="javascript:;" id="addTour" class='t-button'>添加线路</a>
        }
        if (Html.HasUiPermission(this.ViewContext, "muldelete"))
        {
        <a href='javascript:;' class='t-button' onclick='mulDelete()'>删除选择项</a>
        }    
    }
    </text>))
    .Columns(columns =>
    {
        columns.Template(@<text><input name="selectRow" type="checkbox" value="@item.Id " title="checkedRecords" /></text>)
        .HeaderTemplate(@<text><input type="checkbox" title="选择所有" id="checkAllRecords" /></text>)
        .Width(50).HeaderHtmlAttributes(new { style = "text-align:center" }).HtmlAttributes(new { style = "text-align:center" })
        .ClientTemplate("<input type=\"checkbox\" title=\"check all records\" name = 'selectRow' value = '<#= Id #>'/>");
        columns.Bound(o => o.Id).Width(200).Title("系统Id").Hidden();

        columns.Bound(o => o.Name).Width(200).Title("线路名");
        columns.Bound(o => o.Remark).Width(350).Title("简介").EditorTemplateName("TextArea");

        //hidden by yjihrp 2012.7.2.9.44
        columns.Bound(o => o.CustomDateTime).Width(100).Title("自定义时间").Hidden();
        //end hidden by yjihrp 2012.7.2.9.44

        columns.Bound(o => o.UserName).Width(200).Title("添加用户名").ReadOnly().Hidden();
        columns.Bound(o => o.IpAddress).Width(200).Title("添加IP").ReadOnly().Hidden();
        columns.Bound(o => o.AddTime).Width(200).Title("添加时间").Hidden();
        columns.Bound(o => o.State).Width(200).Title("状态").Hidden();
        columns.Bound(o => o.UserId).Width(200).Title("添加用户Id").Hidden();
        columns.Bound(o => o.SortNum).Width(200).Title("排序号").Hidden();
        columns.Bound(o => o.DeleteTime).Width(200).Title("删除时间").Hidden();
        columns.Bound(o => o.IsAud).Width(200).Title("是否审核").Hidden();
        columns.Bound(o => o.IsDelete).Width(200).Title("是否删除").Hidden();
        columns.Bound(o => o.IsTop).Width(200).Title("是否顶置").Hidden();
        columns.Bound(o => o.IsVouch).Width(200).Title("是否推荐").Hidden();
        columns.Bound(o => o.TopTime).Width(200).Title("顶置时间").Hidden();
        columns.Bound(o => o.VouchTime).Width(200).Title("推荐时间").Hidden();
        columns.Bound(o => o.AudTime).Width(200).Title("审核时间").Hidden();

        columns.Template(@<text><a  href = 'javascript:;'  class = 'addTourPic t-button' rel = '@(item.Id)' >配图</a>
    </text>)
        .ClientTemplate("<a  href = 'javascript:;'  class = 'addTourPic t-button' rel = '<#=Id #>' >配图</a> ")
        .Width(100).Title("配置图片").Hidden(!Html.HasUiPermission("Pic", "uploadtour"));

        columns.Template(@<text><a  href = 'javascript:;'  class = 'pretourPic t-button' rel = '@(item.Id)' >浏览配图</a>
    </text>)
        .ClientTemplate("<a  href = 'javascript:;'  class = 'pretourPic t-button' rel = '<#=Id #>' >浏览配图</a> ")
        .Width(100).Title("浏览图片").Hidden(!Html.HasUiPermission(this.ViewContext, "tourpicview"));

        columns.Command(comm =>
        {
            if (Html.HasUiPermission(this.ViewContext, "edit"))
            {
                comm.Edit().ButtonType(GridButtonType.Text);
            }
            if (Html.HasUiPermission(this.ViewContext, "tourdetailindex"))
            {
                comm.Custom("customDetail").Action("tourdetailindex", "tour").SendDataKeys(true).Text("编辑详细");
            }
            if (Html.HasUiPermission(this.ViewContext, "delete"))
            {
                comm.Delete().ButtonType(GridButtonType.Text);
            }
        })
        .Width(300)
        .Title("编辑/删除");
    })
    .ClientEvents(clientEvents => clientEvents.OnDataBinding("dataBinding")
        .OnDataBound("bindAddTourPic").OnComplete("bindComplete"))
    .DataKeys(key => key.Add("Id"))//添加主键
    .DataBinding(dataBinding => dataBinding.Ajax().Select("ajaxindex", "tour")
        .Update("edit", "tour")
        .Delete("delete", "tour"))
    .Editable(editing => editing.Mode(GridEditMode.InLine))
    .Pageable(page => page.PageSize(15))
    .Render();
}
<script type="text/javascript">
    function searchChange() {
        $("#TourGrid").data("tGrid").rebind();
    }

    function dataBinding(args) {
        var searchKeyStr = $("#searchTour").val();
        if (searchKeyStr == '线路名') {
            searchKeyStr = '';
        }
        args.data = $.extend(args.data, { searchKey: searchKeyStr });
    }

    function mulDelete() {
        var delStr = $(':checked');
        if (delStr.length < 1) {
            alert('亲，您没有选择任何记录.');
            return;
        }
        jQuery.post('/tour/muldelete', delStr, function (res) {
            if (res.success) {
                setTimeout(function () {
                    $("#TourGrid").data("tGrid").rebind();
                    $("#checkAllRecords").attr("checked", false);
                }, 1000);
            } else {
                alert(res.message);
            }
        });
    }

    function bindAddTourPic() {
        $('.addTourPic').bind('click', function (e) {
            var targetId = $(this).attr('rel');
            $('#uploadFormTour #tourId').val(targetId);
            e.preventDefault();
            $('#TourAddPicWindow').data('tWindow').center().open();
        });

        $('.pretourPic').bind('click', function (e) {
            var targetId = $(this).attr('rel');
            $.getJSON('/tour/tourpicview/' + targetId, function (res) {
                if (res.success) {
                    var oImg = new Image();
                    oImg.onload = function () {
                        var width = oImg.width;
                        var height = oImg.height;
                        $('#divTourPicPreView').width(width).height(height);
                        $('#divTourPicPreView').parent().width(width).height(height);
                        $('#TourPicPreViewWindow').width(width + 20).height(height + 40);
                        e.preventDefault();
                        $('#divTourPicPreView').html('<img src="' + res.path + '"/>');
                        $('#TourPicPreViewWindow').data('tWindow').center().open();
                    }
                    oImg.src = res.path;
                } else {
                    alert(res.message);
                }
            });
        });
    }

    function bindComplete(e) {
        $("#TourGrid input:checkbox[name='selectRow']").attr("checked", false);
        var success = e.response.success;
        if (success != null && success != undefined) {
            alert(e.response.message);
            window.location.reload();
        }
    }

    $(function () {
        bindAddTourPic();
        $("#TourGrid input:checkbox[name='selectRow']").attr("checked", false);
        $('#addTour').bind('click', function (e) {
            e.preventDefault();
            $('#TourAddWindow').data('tWindow').center().open();
        });

        $('#checkAllRecords').click(function checkAll() { $("#TourGrid tbody input:checkbox[name='selectRow']").attr("checked", this.checked); });
        $(":input[data-autocomplete]").each(function () {
            $(this).autocomplete({
                source: $(this).attr("data-autocomplete")
            });
        });

        $("#searchTour").focus(function () {
            if (this.value == "线路名") {
                $("#searchTour").val("");
            }
        }).blur(function () {
            if (this.value == "") {
                $("#searchTour").val("线路名");
            }
        });
    });
</script>
